import 'dart:math';
import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/common/xindong_methods.dart';
import 'package:xindong/manager/xindong_permission_ctr.dart';
import 'package:xindong/res/assets_res.dart';
import 'package:xindong/widgets/common/xindong_app_bar.dart';
import 'package:xindong/widgets/common/xindong_button.dart';
import 'package:qr_flutter/qr_flutter.dart';

class XinDongInvitePostYeMian extends StatefulWidget {
  final String url;

  const XinDongInvitePostYeMian({super.key, required this.url});

  @override
  State<XinDongInvitePostYeMian> createState() => _XinDongInvitePostYeMianState();
}

class _XinDongInvitePostYeMianState extends State<XinDongInvitePostYeMian> {
  final GlobalKey _globalKey = GlobalKey();
  static const List<String> postList = [AssetsRes.IMAGE_POST1];

  void _onSave() async {
    final storage = await XinDongPermissionCtr.checkPermission(XinDongAppPermission.storage);
    if (!storage) return;
    showLoading();
    try {
      final boundary = _globalKey.currentContext?.findRenderObject() as RenderRepaintBoundary;
      ui.Image image = await boundary.toImage(pixelRatio: 5);
      // 将图像保存到临时目录
      ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
      Uint8List pngBytes = byteData!.buffer.asUint8List();

      // 保存图像到相册
      await ImageGallerySaver.saveImage(pngBytes, quality: 100);
      hideLoading();
      showToast("保存成功");
    } catch (e) {
      mCatchPrint(e);
      hideLoading();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: XinDongAppBar("海报"),
      body: ListView(
        padding: const EdgeInsets.all(36),
        children: [
          Container(
            color: DZColors.white,
            alignment: Alignment.center,
            child: RepaintBoundary(
              key: _globalKey,
              child: Container(
                alignment: Alignment.center,
                // width: 200,
                // height: 262,
                decoration: const BoxDecoration(color: DZColors.white),
                child: Stack(
                  alignment: Alignment.center,
                  children: [
                    Image.asset(postList[Random().nextInt(postList.length)]),
                    Positioned.fill(
                      child: Align(
                        alignment: const Alignment(0.0, 0.68),
                        child: Container(
                          color: Colors.white,
                          padding: const EdgeInsets.all(5.0),
                          child: QrImageView(
                            data: widget.url,
                            size: 76,
                            padding: EdgeInsets.zero,
                            backgroundColor: DZColors.white,
                          ),
                        ),
                      ),
                    )
                  ],
                ),
              ),
            ),
          ),
          const SizedBox(height: 20),
          SizedBox(
            height: kButtonHeight,
            child: XinDongCommonButton("保存二维码", onTap: _onSave),
          ),
        ],
      ),
    );
  }
}
